package com.okami.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author: TanJingyu
 * @create: 2021-02-18 17:42
 **/
@RequestMapping("/hello")
@Controller
public class HelloController {
    @Value("${user}")
    private String userName;

    @GetMapping("/codert")
    public void codert(HttpServletResponse response) throws IOException {
        response.addHeader("WWW-Authenticate", "Basic realm=\"" + 1111 + "\"");
        response.sendError(HttpStatus.UNAUTHORIZED.value(), HttpStatus.UNAUTHORIZED.getReasonPhrase());
    }

    @RequestMapping
    @ResponseBody
    public synchronized String hello(HttpSession session) {
        System.out.println(session);
        return userName;
    }

    @RequestMapping("/hellopage")
    @ResponseBody
    public String hellopage(Pageable pageable) {
        return "你好啊";
    }

    public class Hello {
        private String name;
    }

    @PostMapping("post")
    public String post(@RequestBody Dto dto) {
        System.out.println(dto);
        return "成了";
    }

    public static class Dto {
        private String name;
        private DtoInner son;

        public DtoInner getSon() {
            return son;
        }

        public void setSon(DtoInner son) {
            this.son = son;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

    public static class DtoInner {
        private String address;

        public String getAddress() {
            return address;
        }

        public void setAddress(String address) {
            this.address = address;
        }
    }
}
